<?php

/**
 * an specific ad
 * @property string $productType type of product
 * @property string $manufacturer manufacturer
 */
class Equipment extends Ad {

    public $productType = 'press';
    public $manufacturer = '';

    /**
     * combine rules
     * @return array
     */
    public function rules() {
	return CMap::mergeArray(parent::rules(), array(
		    array('productType, contact_name, contact_email, manufacturer', 'required'),
		    array('productType', 'in', 'range' => array('press', 'pre-press', 'finishing', 'parts', 'supplies'))
	));
    }

    function defaultScope() {
	return array(
	    'condition' => "`type`= " . self::TYPE_EQUIPMENT,
	);
    }

    /**
     * rewrite type
     */
    public function beforeValidate() {
	parent::beforeValidate();

	$this->type = self::TYPE_EQUIPMENT;

	return true;
    }

    /**
     * before save action
     */
    public function afterSave() {
	parent::afterSave();

	if ($this->isNewRecord) {
	    $meta = new AdMeta();

	    //add product type to meta
	    $meta->key = 'productType';
	    $meta->value = $this->productType;
	    $this->addMeta($meta);

	    $meta = new AdMeta();
	    //add manufacturer to meta
	    $meta->key = 'manufacturer';
	    $meta->value = $this->manufacturer;
	    $this->addMeta($meta);
	} elseif ($this->scenario == self::SCENARIO_UPDATE) {
	    //update ad meta
	    $productTypeMeta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'productType'
	    ));
	    if (!$productTypeMeta) {
		$productTypeMeta = new AdMeta();
		$productTypeMeta->key = 'productType';
	    }
	    $productTypeMeta->value = $this->productType;
	    $productTypeMeta->save();

	    unset($productTypeMeta);

	    //update manufacturer
	    $manufacturerMeta = AdMeta::model()->findByAttributes(array(
		'ad_id' => $this->ad_id,
		'key' => 'manufacturer'
	    ));
	    if (!$manufacturerMeta) {
		$manufacturerMeta = new AdMeta();
		$manufacturerMeta->key = 'manufacturer';
	    }
	    $manufacturerMeta->value = $this->manufacturer;
	    $manufacturerMeta->save();

	    unset($manufacturerMeta);
	}
    }

    /**
     * action after find
     */
    public function afterFind() {
	parent::afterFind();

	//render meta
	$metaData = app()->cache->get('ad_meta_data_' . $this->ad_id);
	if (!$metaData) {
	    $metaData = AdMeta::model()->findAll('ad_id = ' . $this->ad_id);
	}

	//bind value
	foreach ($metaData as $meta) {
	    if ($meta->key == 'productType') {
		$this->productType = $meta->value;
	    } elseif ($meta->key == 'manufacturer') {
		$this->manufacturer = $meta->value;
	    }
	}
    }

    public static function getTypes() {
	return array(
	    'press' => 'Press',
	    'pre-press' => 'Pre-press',
	    'finishing' => 'Finishing',
	    'parts' => 'Parts',
	    'supplies' => 'Supplies'
	);
    }

    public function getProductType() {
	switch ($this->productType) {
	    case 'press' : return 'Press';
	    case 'pre-press' : return 'Pre press';
	    case 'finishing' : return 'Finishing';
	    case 'parts' : return 'Parts';
	    case 'supplies' : return 'Supplies';
	}
    }

    /**
     * get text of price
     * @return string
     */
    public function getPrice() {
	if ($this->price > 0) {
	    return '$' . number_format($this->price, 2);
	}

	return 'All reasonable offers considered';
    }

    /*
     * 
     */

    public function afterDelete() {
	parent::afterDelete();
	if ($this->image) {
	    deleteFile(UPLOAD_PATH . $this->image);
	}
    }

}

